package com.ctw.mapmycity.util;

import com.ctw.mapmycity.R;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

public class MaskUtil {
	
	private Activity mActivity;
	private View mStatusView;
	private TextView mStatusMessageView;
	
	public MaskUtil(final Activity activity) {
		this.mActivity = activity;
		this.mStatusView = activity.findViewById(R.id.mask);
		this.mStatusMessageView = (TextView) activity.findViewById(R.id.login_status_message);
	}
	
	public void unmask(final View view) {
		process(false, view, -1);
	}
	
	public void mask(final View view, final int stringId) {
		process(true, view, stringId);
	}
	
	/**
	 * Shows the progress UI and hides the login form.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	private void process(final boolean show, final View view, final int stringId) {
//		if (stringId >= 0)
//			mStatusMessageView.setText(stringId);
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			int shortAnimTime = this.mActivity.getResources().getInteger(android.R.integer.config_shortAnimTime);

			mStatusView.setVisibility(View.VISIBLE);
			mStatusView.animate().setDuration(shortAnimTime).alpha(show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationEnd(Animator animation) {
					mStatusView.setVisibility(show ? View.VISIBLE: View.GONE);
				}
			});

			view.setVisibility(View.VISIBLE);
			view.animate().setDuration(shortAnimTime).alpha(show ? 0:1).setListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationEnd(Animator animation) {
					view.setVisibility(show ? View.GONE:View.VISIBLE);
				}
			});

		} else {
			// The ViewPropertyAnimator APIs are not available, so simply show and hide the relevant UI components.
			mStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
			view.setVisibility(show ? View.GONE : View.VISIBLE);
		}
	}
}

